package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 演示验证码功能
 * @author 千锋健哥
 */
@Controller
@RequestMapping("/code")
public class TestCodeController {

    /**
     * 跳转到验证码页面
     * @return
     */
    @RequestMapping("/test1")
    public String jumpCode() {
        return "code";
    }

    /**
     * 校验验证码是否正确
     * @return
     */
    @RequestMapping("/test2")
    @ResponseBody
    public String checkCode(String captcha, HttpSession session) {
        //1. 从session中获取保存的验证码内容
        String sessionCode = (String)session.getAttribute("captcha");
        //2. 判断用户输入的验证码是否正确
        if (sessionCode.equalsIgnoreCase(captcha)){
            return "success";
        }
        //3. 返回结果
        return "error";

    }
}
